package cn.com.callback

import groovy.json.*;
import groovy.swing.impl.DefaultAction
import org.springframework.jdbc.core.JdbcTemplate

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.RequestDispatcher;
import cn.com.callback.CallBackInvoke
import cn.com.callback.ServiceBackInvoke
import cn.com.elite.EliteBean;
import cn.com.elite.Util;
import java.security.MessageDigest;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import cn.com.kh.getProperty;
import cn.com.kh.KHUtil;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.json.JSONException;

/**
 *
 * @Title: ApiBPEnum* @Description:深圳宝安排水小程序登录接口--外部接口
 * @author: chieh* @date 2020-11-08 21:28:00
 *
 */
public class ApiBPEnum implements ServiceBackInvoke {
    private static final Log log = LogFactory.getLog(ApiBPEnum.class);

    public String doCallBack(EliteBean eliteBean, HttpServletRequest request, HttpServletResponse response) {

        log.debug("-------------------------start ApiBPEnum 开始-----------------");

        long startTime = System.currentTimeMillis();
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json;charset=UTF-8");
        JdbcTemplate jt = new JdbcTemplate(eliteBean.getDataSource());
        String reJson = null;
        long endTime = 0;

        //获取参数
        def usetype = request.getParameter("usetype");
        def pid = request.getParameter("pid");
        log.debug("usetype：" + usetype + "	pid：" + pid);

        try {
            if (usetype == null) {
                endTime = System.currentTimeMillis();
                reJson = "{\"resultcode\":\"0\",\"resultmessage\":\"处理失败(参数输入异常),调用问题处理时长(毫秒):" + (endTime - startTime) + "\",\"data\":[]}";
                return reJson;
            }

            // 查询加载字典数据
            List<Map<String, Object>> listcountresult = null;
            if (KHUtil.isNull(pid)) {
                String selectSQL = "Select Concat(Code_Id, '|', Code_Name) As Result From c_Codetables_Wxm Where Use_Type = ? And Use_Status = 1 Order By Orderno";
                listcountresult = jt.queryForList(selectSQL, usetype);

            } else {
                String selectSQL = "Select Concat(Code_Id, '|', Code_Name) As Result From c_Codetables_Wxm Where Use_Type = ? And Pcode_Id = ? And Use_Status = 1 Order By Orderno";
                listcountresult = jt.queryForList(selectSQL, usetype, pid);

            }

            int count = listcountresult.size();
            log.debug("查询加载字典数据-Count：" + count);

            def resultString = "";
            def str = "";
            if (count > 0) {
                for (int i = 0; i < count; i++) {
                    resultString = listcountresult.get(i).get("Result");
                    str = str + resultString + ',';
                }
                str = str.substring(0, str.length() - 1);
            }
            log.debug("拼接字典结果String：" + str);

            JSONArray jsonArray = new JSONArray();
            // json对象
            JSONObject json = new JSONObject();
            json.put("key", str);

            jsonArray.add(json);

            endTime = System.currentTimeMillis();
            reJson = "{\"resultcode\":\"1\",\"resultmessage\":\"获取的枚举值接口调用处理时长(毫秒):" + (endTime - startTime) + "\",\"data\":" + jsonArray.toString() + "}";

        } catch (Exception e) {
            log.error("接口处理异常" + e.getMessage());
            // 失败返回
            reJson = "{\"resultcode\":\"0\",\"resultmessage\":\"获取的枚举值接口调用处理失败,请检查确认入参有效且合法\",\"data\":[]}";

        }
        return reJson;

    }
}